import datetime
from copy import copy
from django.contrib.auth import logout
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import *
from django.template import Context
from django.template.loader import get_template

from wiki.forms import DeleteWikiForm, EditWikiForm
from wiki.models import Page
from wiki.side_bar import make_side_bar_data

def delete_page(request, page_title):
    try:
        page = Page.objects.get(title=page_title)
        side_bar = make_side_bar_data(Page.objects.all())
    except:
        raise Http404('not found.')

    if request.method == 'POST' and request.POST['confirm'] == '/':
        page.delete()
        return HttpResponseRedirect('/')
    else:
        form = DeleteWikiForm()
        variables = Context({
            'page_title': page_title,
            'form': form,
            'side_bar': side_bar
        })
        return render_to_response(
            'delete_page.html',
            variables
        )

def logout_page(request) :
	logout(request)
	return HttpResponseRedirect('/')

def edit_page(request, title):
    if request.method == 'POST':
        if title == '':
            title = request.POST['title']
            content = request.POST['content']
            page = Page(title=title, content=content)
        else:
            page = Page.objects.get(title=title)
            page.content = request.POST['content']
            page.update_date = datetime.datetime.now()
        page.author = request.user.email
        page.save()
        return HttpResponseRedirect('/view/' + title)
    else:
        side_bar = make_side_bar_data(Page.objects.all())
        if title == '':
            return render_to_response(
                    'edit_page.html',{
                    'exist': 'not',
                    'side_bar': side_bar,
                    'form' : EditWikiForm()
                    }
            )
        else:
            try:
                page = Page.objects.get(title=title)
            except:
                page = Page.objects.set(title=title)  
            #pages = Page.objects.all()
            template = get_template('edit_page.html')
            variables = Context({
                    'update_date': page.update_date,
                    'title': title,
                    'side_bar': side_bar,
                    'form' : EditWikiForm({
                        'title' : page.title,
                        'content' : page.content,
                        'author' : page.author
                    })
            })
            output = template.render(variables)
            return HttpResponse(output)

def list_page(request):
    try:
        pages = Page.objects.all()
        side_bar = make_side_bar_data(pages)
    except:
        raise Http404('Page Not found!')

    return render_to_response(
        'list_page.html',
        {   'user' : request.user,
            'pages' : pages,
            'side_bar': side_bar
        }
    )

def view_page(request, title):
    try:
        page = Page.objects.get(title=title)
        side_bar = make_side_bar_data(Page.objects.all())
    except:
        raise Http404('Page Not found!')

    template = get_template('view_page.html')
    variable = Context({
        'page_title': page.title,
        'page_content': page.content,
        'page_date': page.update_date,
        'side_bar': side_bar
    })

    output = template.render(variable)
    return HttpResponse(output)
